package system;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import object.model.MySQLData;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import system.mysql.schema.information.models.COLLATION_CHARACTER_SET_APPLICABILITY;
import system.mysql_connector.MySQL_Connector;
import system.queryer.MySQL_Queryer;

import com.mysql.opt.Operator;


class SysFactory {
	
	private static SysFactory curInstance;
	
	private SysFactory(){
		
	}
	
	public static SysFactory getInstance(){
		if(curInstance==null){
			curInstance = new SysFactory();
		}
		return curInstance;
	}
	
	public String getProperty(String propertyName){
		String val = null;
		try{
			File conf = new File(Sys_Config.config_xml_path);
			DocumentBuilderFactory dbF = DocumentBuilderFactory.newInstance();		
			DocumentBuilder builder = dbF.newDocumentBuilder();
			Document doc = builder.parse(conf);
			NodeList nodeL = doc.getElementsByTagName(propertyName);
			if(nodeL.getLength()==1){
				val = nodeL.item(0).getTextContent();
			}
		}catch(Exception ee){
			ee.printStackTrace();
		}		
		return val;
	}
	
	public Map<String, MySQLData> getBunchProperties(String propertyName){
		String val = null;
		//MAP: K=>Field[String], V=Type[MySQLData]
		Map<String, MySQLData> map = new HashMap<String, MySQLData>();
		try{
			InputStream conf = Sys_Config.mysql_data_stream;
			DocumentBuilderFactory dbF = DocumentBuilderFactory.newInstance();		
			DocumentBuilder builder = dbF.newDocumentBuilder();
			Document doc = builder.parse(conf);
			NodeList nodeL = doc.getElementsByTagName(propertyName);
			for(int i=0;i<nodeL.getLength();i++){
				Node node = nodeL.item(i);
				NamedNodeMap nnm = node.getAttributes();				
				String name = nnm.getNamedItem("name").getNodeValue();
				String ref = nnm.getNamedItem("ref").getNodeValue();
				String format = nnm.getNamedItem("format").getNodeValue();			
				//ini map
				map.put(name, new MySQLData(name, ref, format));
			}			
		}catch(Exception ee){
			ee.printStackTrace();
		}		
		return map;		
	}

	public Map<String, String> getCollationRel() {
		// TODO Auto-generated method stub
		MySQL_Queryer mysqlQ = new MySQL_Queryer();
		List<COLLATION_CHARACTER_SET_APPLICABILITY> relList = mysqlQ.toFind("SELECT * FROM information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY`", new COLLATION_CHARACTER_SET_APPLICABILITY(), null, null);
		Map<String, String> map = new HashMap<String, String>();
		for(int i=0;i<relList.size();i++){
			COLLATION_CHARACTER_SET_APPLICABILITY item = relList.get(i);
			map.put(item.getCOLLATION_NAME(), item.getCHARACTER_SET_NAME());
		}
		return map;
	}

	public Map<String, String> getBackupConfigureMap()
	{
		Map<String, String>	map = new HashMap<String, String>();
		try{
			DocumentBuilderFactory dbF = DocumentBuilderFactory.newInstance();		
			DocumentBuilder builder = dbF.newDocumentBuilder();
			Document doc;
			doc = builder.parse(Sys_Config.backup_head_format);
			//Get Seperator
			NodeList nodeL = doc.getElementsByTagName("itemsperator");
			Node node = nodeL.item(0);
			NamedNodeMap nnm = node.getAttributes();
			map.put("SEPERATOR", nnm.getNamedItem("value").getNodeValue());
			
			//Get Header Content
			String header_content;
			NodeList HnodeL = doc.getElementsByTagName("format");
			Node Hnode = HnodeL.item(0);
			NamedNodeMap Hnnm = Hnode.getAttributes();
			String name = Hnnm.getNamedItem("name").getNodeValue();
			String vers = Hnnm.getNamedItem("version").getNodeValue();
			header_content = Hnode.getTextContent();
			header_content = header_content.replace("[next-line]", Operator.nextline);
			header_content = header_content.replace("[name]", name);	
			header_content = header_content.replace("[version]", vers);	
			header_content = header_content.replace("[DB_ADDRESS]", Sys_Config.DB_ADDRESS);
			header_content = header_content.replace("[DB_NAME]", Sys_Config.DB_NAME);
			header_content = header_content.replace("[DB_DEFAULT_ENCODING]", Sys_Config.DB_DEFAULT_ENCODING);
			header_content = header_content.replace("[DB_USERNAME]", Sys_Config.DB_USERNAME);
			header_content = header_content.replace("[DB_PORT]", Sys_Config.DB_PORT);
			map.put("HEADER_CONTENT", header_content);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}
	

	public Map<String, String> getDatabaseMetaDataMap() {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<String, String>();
		DatabaseMetaData metaData = null;
		try {
			Connection conn = MySQL_Connector.getConnection();
			metaData = conn.getMetaData();
			map.put(Sys_Config.KEY_PROCDUCT_NAME, metaData.getDatabaseProductName());
			map.put(Sys_Config.KEY_PROCDUCT_VER, metaData.getDatabaseProductVersion());
			map.put(Sys_Config.KEY_DRIVER_NAME, metaData.getDriverName());
			map.put(Sys_Config.KEY_DRIVER_VER, metaData.getDriverVersion());
			MySQL_Connector.closeConnection(conn);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(0);
		}
		return map;
	}
	
}
